//
//  MYNaviView.m
//  QKManager
//
//  Created by ukongm on 17/8/12.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "MYNaviView.h"



@interface MYNaviView ()

/** 标题 */
@property(nonatomic,strong)UILabel* titleLb;
/** 左边的最后一个按钮 */
@property(nonatomic,strong)UIButton* leftLastBtn;
/** 右边的最后一个按钮 */
@property(nonatomic,strong)UIButton* rightLastBtn;

@end


@implementation MYNaviView

- (UILabel *)titleLb{
    
    if (_titleLb == nil) {
        _titleLb = [[UILabel alloc] init];
        
        _titleLb.textColor = [UIColor whiteColor];
        
        _titleLb.font = [UIFont boldSystemFontOfSize:17];
        
        _titleLb.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLb;
}

- (void)addUI{
    [self addSubview:self.titleLb];
    
    typeof(self) sself = self;
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.mas_equalTo(-20);
        make.width.greaterThanOrEqualTo(@0);
        make.bottom.equalTo(sself);
        make.centerX.equalTo(sself);
    }];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addUI];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addUI];
    }
    return self;
}

- (void)setTitle:(NSString *)title{
    _title = title;
    self.titleLb.text = title;
}

- (void)setLeftBtns:(NSArray *)leftBtns{
    
    [_leftBtns makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    _leftBtns = leftBtns;
    
    [self setItemForArray:leftBtns left:YES];
}

- (void)setRightBtns:(NSArray *)rightBtns{
    [_rightBtns makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    _rightBtns = rightBtns;
    
    [self setItemForArray:rightBtns left:NO];
}


- (void)setItemForArray:(NSArray*)items left:(BOOL)left{
    
    UIButton* lastItem = nil;
    for (UIButton* item in items) {
        
        [self addSubview:item];
        
        CGFloat width = item.width;
        CGFloat height = item.height;
        
        if (width == 0) {
            width = 30;
        }
        
        if(height == 0){
            height = 30;
        }
        
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            
            if (lastItem) {
                if (left) {
                    make.left.equalTo(lastItem.mas_right).offset(10);
                }else{
                    make.right.equalTo(lastItem.mas_left).offset(-10);
                }
            }else{
                if (left) {
                    make.left.equalTo(self).offset(15);
                }else{
                    make.right.equalTo(self).offset(-15);
                }
            }
            
            make.centerY.equalTo(self.titleLb);
            
            make.width.mas_equalTo(width);
            make.height.mas_equalTo(height);
        }];
        
        lastItem = item;
    }
}


@end
